package life.mahu.community.controller;

import life.mahu.community.cache.HotTagCache;
import life.mahu.community.dto.PaginationDTO;
import life.mahu.community.dto.ResultDTO;
import life.mahu.community.model.Category;
import life.mahu.community.service.CategoryService;
import life.mahu.community.service.QuestionService;
import life.mahu.community.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Controller
public class IndexController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private HotTagCache hotTagCache;

    @Autowired
    private CategoryService categoryService;

    @Resource
    private RedisUtil redis;

    private static int ExpireTime = 60*60;   // redis中存储的过期时间s

    @GetMapping("/")
    public String index(Model model,
                        @RequestParam(name = "page", defaultValue = "1") Integer page,
                        @RequestParam(name = "size", defaultValue = "10") Integer size,
                        @RequestParam(name = "search", required = false) String search,
                        @RequestParam(name = "tag", required = false) String tag,
                        @RequestParam(name = "category", required = false, defaultValue = "0") Integer category,
                        @RequestParam(name = "sort", required = false, defaultValue = "new") String sort) {

        // 查询分类
        List<Category> categoryList = categoryService.getList();

        if((tag==null || "".equals(tag)) && category == 0){
            category = categoryList.get(0).getId();
        }

        // 按热度及分类查找问题贴
        PaginationDTO pagination = questionService.list(search, tag, category, sort, page, size);

        // 查询热门标签
        List<String> tags;
        if (redis.hasKey("mahuHotTags")) {
            tags =  (List<String>)(List)redis.lGet("mahuHotTags", 0, -1);
        }else{
            tags = hotTagCache.getHots();
            if(tags.size() != 0)
                redis.listRSet("mahuHotTags", (List<Object>)(List)tags, ExpireTime);
        }
        model.addAttribute("pagination", pagination);
        model.addAttribute("search", search);
        model.addAttribute("tag", tag);
        model.addAttribute("tags", tags);
        model.addAttribute("categoryId", category);
        model.addAttribute("categorys", categoryList);
        model.addAttribute("sort", sort);
        return "index";
    }

    @ResponseBody
    @RequestMapping(value = "/getCategory", method = RequestMethod.POST)
    private ResultDTO category(){

        // 查询分类
        List<Category> categoryList;
        if(redis.hasKey("mahuCategory")){
            categoryList = (List<Category>)(List)redis.lGet("mahuCategory", 0, -1);
        }else{
            categoryList = categoryService.getList();
            if(categoryList.size() != 0) {
                redis.del("mahuCategory");
                redis.listRSet("mahuCategory", (List<Object>) (List) categoryList, ExpireTime);
            }
        }

        return ResultDTO.okOf(categoryList);
    }

    @ResponseBody
    @RequestMapping(value = "/getTag", method = RequestMethod.POST)
    private ResultDTO getTag(){

        Set<String> tagList;
        if(redis.hasKey("mahuTag")){
            tagList = (Set<String>)(Set)redis.sGet("mahuTag");
        }else{
            tagList = categoryService.getTagList();
            if(tagList.size() != 0){
                for (String tag : tagList) {
                    redis.sSetAndTime("mahuTag",  ExpireTime, (String)tag);
                }
            }
        }

        return ResultDTO.okOf(tagList);
    }


}
